package com.mt.course.service.model;

import java.util.List;
import java.util.Map;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.mt.common.PagerInfo;
import com.mt.common.ServiceResult;
import com.mt.course.entity.ExaminationEntity;
import com.mt.course.mapper.ExaminationEntityMapper;

@Component
public class ExaminationServiceModel {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ExaminationServiceModel.class);

    @Autowired
    private ExaminationEntityMapper examinationEntityMapper;

    public ServiceResult<List<ExaminationEntity>> selectList(Map<String, Object> params, PagerInfo<?> pagerInfo) {
        Assert.notNull(this.examinationEntityMapper, "Property 'examinationEntityMapper' is required.");
        ServiceResult<List<ExaminationEntity>> result = new ServiceResult<List<ExaminationEntity>>();
        try {
            if (pagerInfo != null) {
                PageHelper.startPage(pagerInfo.getPageIndex(), pagerInfo.getPageSize());
            }

            result.setResult(this.examinationEntityMapper.selectList(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ExaminationServiceModel][selectList]：query selectList occur exception", e);
        }
        return result;
    }

    public ServiceResult<ExaminationEntity> selectInfo(Map<String, Object> params) {
        Assert.notNull(this.examinationEntityMapper, "Property 'examinationEntityMapper' is required.");
        ServiceResult<ExaminationEntity> result = new ServiceResult<ExaminationEntity>();
        try {
            result.setResult(this.examinationEntityMapper.selectByPrimaryKey(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ExaminationServiceModel][selectInfo]：query selectInfo occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> selectCount(Map<String, Object> params) {
        Assert.notNull(this.examinationEntityMapper, "Property 'examinationEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int count = 0;
        try {
            count = this.examinationEntityMapper.selectCount(params);
            result.setResult(count);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ExaminationItemCollectServiceModel][selectCount]：query selectCount occur exception", e);
            throw new RuntimeException(e.getMessage());
        }
        return result;
    }

    public ServiceResult<Integer> insert(ExaminationEntity examinationEntity) {
        Assert.notNull(this.examinationEntityMapper, "Property 'examinationEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.examinationEntityMapper.insert(examinationEntity);
            if (save > 0) {
                id = examinationEntity.getExaminationId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ExaminationServiceModel][insert]：query insert occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> updateRelate(ExaminationEntity examinationEntity) {
        Assert.notNull(this.examinationEntityMapper, "Property 'examinationEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.examinationEntityMapper.updateByPrimaryKeySelective(examinationEntity);
            if (save > 0) {
                id = examinationEntity.getExaminationId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ExaminationServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> updateByPrimaryKeySelective(ExaminationEntity examinationEntity) {
        Assert.notNull(this.examinationEntityMapper, "Property 'examinationEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.examinationEntityMapper.updateByPrimaryKeySelective(examinationEntity);
            if (save > 0) {
                id = examinationEntity.getExaminationId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ExaminationServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> sumOneCourseQuestionCount(Map<String, Object> params) {
        Assert.notNull(this.examinationEntityMapper, "Property 'examinationEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            int save = this.examinationEntityMapper.sumOneCourseQuestionCount(params);
            result.setResult(save);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error(
                    "[ExaminationServiceModel][sumOneCourseQuestionCount]:query sumOneCourseQuestionCount occur exception",
                    e);
        }
        return result;
    }

    public ServiceResult<Integer> deleteByPrimaryKey(Integer examinationId) {
        Assert.notNull(this.examinationEntityMapper, "Property 'examinationEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            int save = this.examinationEntityMapper.deleteByPrimaryKey(examinationId);
            result.setResult(save);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ExaminationServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }
}
